﻿Imports System.Data.OleDb

Public Class EyeDetail
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub backbtn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles backbtn.Click
        Response.Redirect("Eyelist.aspx")
    End Sub


    Protected Sub btnCmmnt_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCmmnt.Click
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("MacrgraphyConnectionString").ConnectionString)


        'sql = "insert into Comments(UserName,Comments,Img_Id) values (@f1,@f2,@f3)"
        Dim sql As String = "INSERT INTO Comments2(ViewerName, Img_Id,Comments) VALUES (@f1,@f2,@f3)"
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        'cmd.CommandType = CommandType.Text
        conn.Open()
        cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        'cmd.Parameters.AddWithValue("@f1", txtViewer.Text)
        cmd.Parameters.AddWithValue("@f2", Request.Params("Img_Id"))
        cmd.Parameters.AddWithValue("@f2", txtCmmnt.Text)

        cmd.ExecuteNonQuery()
        conn.Close()
        Response.Redirect("Eyelist.aspx")

    End Sub

    Protected Sub BtnRating_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BtnRating.Click
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("MacrgraphyConnectionString").ConnectionString)
        Dim Rsql = "DELETE FROM Rating WHERE Img_Id=@f1 AND Rater=@f3"
        Dim dCmd As New OleDbCommand(Rsql, conn)

        dCmd.Parameters.AddWithValue("f1", Request.Params("Img_Id"))
        dCmd.Parameters.AddWithValue("f3", User.Identity.Name)
        dCmd.ExecuteNonQuery()

        Dim sql = "insert into Rating(Img_Id,Rating) values (@f1,@f2)"
        Dim addCmd As New OleDbCommand(sql, conn)
        conn.Open()
        addCmd.Parameters.AddWithValue("f1", Request.Params("Img_Id"))
        addCmd.Parameters.AddWithValue("f2", Rating1.CurrentRating)
        addCmd.ExecuteNonQuery()
        conn.Close()
        Response.Redirect("Eyelist.aspx")
    End Sub

    Protected Sub TagBtn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles TagBtn.Click
        Dim Img_Id = Request.Params("Img_Id")

        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("MacrgraphyConnectionString").ConnectionString)

        conn.Open()

        Dim NewTags = TxtTags.Text.Split(",")

        For Each newTag As String In NewTags

            Dim isTagThereCmd As New OleDbCommand(" SELECT * FROM Tags WHERE Tags=@f2", conn)
            isTagThereCmd.Parameters.AddWithValue("@f2", newTag)
            Dim dr = isTagThereCmd.ExecuteReader()
            If Not dr.HasRows Then
                Dim addTagcmd As New OleDbCommand(" INSERT INTO Tags (Tags) VALUES (@f2)", conn)
                addTagcmd.Parameters.AddWithValue("@f2", newTag)
                addTagcmd.ExecuteNonQuery()
            End If
            Dim FindTagCmd As New OleDbCommand("SELECT Tag_Id From Tags WHERE Tags=@f2", conn)
            FindTagCmd.Parameters.AddWithValue("@f2", newTag)
            Dim Tagdr = FindTagCmd.ExecuteReader()
            Tagdr.Read()
            Dim NewTagId = Tagdr.Item("Tag_Id")
            Dim AssociationCmd As New OleDbCommand("insert into ImageByTag(ImageFk,TagFk) values (@f1,@f2)", conn)
            AssociationCmd.Parameters.AddWithValue("@f2", Img_Id)
            AssociationCmd.Parameters.AddWithValue("@f1", NewTagId)

            AssociationCmd.ExecuteNonQuery()
        Next



        For Each cb As ListItem In TagChkList.Items
            If (cb.Selected = True) Then
                Dim sql = "insert into ImageByTag(ImageFk,TagFk) values (@f1,@f2)"
                Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
                cmd.CommandType = CommandType.Text
                cmd.Parameters.AddWithValue("@f1", Request.Params("Img_Id"))
                cmd.Parameters.AddWithValue("@f2", cb.Value)
                cmd.ExecuteNonQuery()

            End If

        Next
        Response.Redirect("Eyelist.aspx")
    End Sub
End Class